package com.dudu.controller;
import com.dudu.dao.AssetsMapper;
import com.dudu.domain.Assets;
import com.dudu.domain.Assettype;
import com.dudu.domain.Smallassetstype;
import com.dudu.service.AssetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/** 
 * Created by tengj on 2017/3/13.
 */
@Controller
@RequestMapping("/assets")
public class AssetsController {

    @Autowired
    private AssetsService assetsService;
    @Autowired
    AssetsMapper assetsMapper;

    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }	
    
    @RequestMapping("/list")
    @ResponseBody
	public Map<String, List<Assets>> selectAll() {
		List<Assets> assetslist=assetsService.getAllAssets();
		for (int i = 0; i < assetslist.size(); i++) {
			System.out.println(assetslist.get(i).getName());
		}
		Map<String, List<Assets>> map=new HashMap<>();
		map.put("assetsList", assetslist);
	    return map;
	}

    
    @RequestMapping("/listByroomid")
    @ResponseBody
    public ModelAndView AssetsListbyRoomId(Integer id){
        List<Assets> assetsList=assetsService.getAssetsbyRoomId(id);
        
        ModelAndView modelAndView = new ModelAndView("assetsList");
        modelAndView.addObject("assetslist", assetsList);
        
        return modelAndView;
    }
    
    @RequestMapping("/typelist")
    @ResponseBody
    public ModelAndView type( ){
    	
    List<Smallassetstype> smallassetstypes=assetsService.getAllSmallassetstype();
    List<Assettype> assettypes =assetsService.getAllAssettype();
    ModelAndView modelAndView = new ModelAndView("typeList");
    modelAndView.addObject("smallassetstypes", smallassetstypes);
    modelAndView.addObject("assettypes", assettypes);
    return modelAndView;
    }
    @RequestMapping("/toAdd")
    @ResponseBody
    public ModelAndView toAdd( ){
        List<Smallassetstype> smallassetstypes=assetsService.getAllSmallassetstype();
        List<Assettype> assettypes =assetsService.getAllAssettype();
        ModelAndView modelAndView = new ModelAndView("addAsset");
        modelAndView.addObject("smallassetstypes", smallassetstypes);
        modelAndView.addObject("assettypes", assettypes);
        return modelAndView;
    }    
    @RequestMapping("/save")
    @ResponseBody
    public Map<String, List<Assets>> save(Assets assets ){
    	int  result=assetsService.addAssets(assets);
      return this.selectAll();
    } 
   
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, List<Assets>> delete(String  id ){
    	int  result=assetsService.deletAssets(id);
      return this.selectAll();
    }
    @RequestMapping("/toupdate")
    @ResponseBody
    public ModelAndView toupdate(String  id){
        List<Smallassetstype> smallassetstypes=assetsService.getAllSmallassetstype();
        List<Assettype> assettypes =assetsService.getAllAssettype();
        ModelAndView modelAndView = new ModelAndView("updateAsset");
        Assets assets=assetsService.selectAssetsById(id);
        modelAndView.addObject("smallassetstypes", smallassetstypes);
        modelAndView.addObject("assets", assets);
        return modelAndView;
    }     
}